<?php

use yii\db\Migration;

/**
 * Class m171206_061735_createTableCatalog
 */
class m171206_061735_createTableCatalog extends Migration
{
    /**
     * @inheritdoc
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%course_catalog}}', [

            'id' => $this->string(50)->notNull()->defaultValue('')->comment('目录ID'),
            'user_id' => $this->integer()->notNull()->defaultValue(0)->comment('用户ID'),
            'course_id' => $this->string(50)->notNull()->defaultValue('')->comment('课程ID'),
            'parent_id' => $this->string(50)->notNull()->defaultValue('0')->comment('父ID'),

            'title' => $this->string()->notNull()->defaultValue('')->comment('目录名称'),
            'is_lock' => $this->smallInteger()->notNull()->defaultValue(0)->comment('目录锁定 0-未锁定 1-锁定'),
            'ordernum' => $this->integer()->notNull()->defaultValue(0)->comment('排序号'),
            'tier' => $this->smallInteger()->notNull()->defaultValue(0)->comment('层级'),

            'status' => $this->smallInteger()->notNull()->defaultValue(10)->comment('状态'),
            'created_at' => $this->integer()->notNull()->defaultValue(0)->comment('创建时间'),
            'updated_at' => $this->integer()->notNull()->defaultValue(0)->comment('更新时间'),
        ], $tableOptions);

        $this->addCommentOnTable('{{%course_catalog}}','课程目录表');

        $this->addPrimaryKey('course_catalog_id','{{%course_catalog}}','id');
    }

    /**
     * @inheritdoc
     */
    public function safeDown()
    {
        $this->dropTable('{{%course_catalog}}');
    }


    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m171206_061735_createTableCatalog cannot be reverted.\n";

        return false;
    }
    */
}
